<?php
/**
 * Class that operate on table 'CONTACT'. Database Mysql.
 *
 * @author: Ali
 * @date: 2012-04-01 17:54
 */
class CONTACTMySqlDAO implements CONTACTDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return CONTACTMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM CONTACT WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM CONTACT';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM CONTACT ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param cONTACT primary key
 	 */
	public function delete($id){
		$sql = 'DELETE FROM CONTACT WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param CONTACTMySql cONTACT
 	 */
	public function insert($cONTACT){
		$sql = 'INSERT INTO CONTACT (HEADING_EN, NAME_EN, EMAIL_EN, TEXT_EN, BUTTON_EN, HEADING_SV, NAME_SV, EMAIL_SV, TEXT_SV, BUTTON_SV) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($cONTACT->hEADINGEN);
		$sqlQuery->set($cONTACT->nAMEEN);
		$sqlQuery->set($cONTACT->eMAILEN);
		$sqlQuery->set($cONTACT->tEXTEN);
		$sqlQuery->set($cONTACT->bUTTONEN);
		$sqlQuery->set($cONTACT->hEADINGSV);
		$sqlQuery->set($cONTACT->nAMESV);
		$sqlQuery->set($cONTACT->eMAILSV);
		$sqlQuery->set($cONTACT->tEXTSV);
		$sqlQuery->set($cONTACT->bUTTONSV);

		$id = $this->executeInsert($sqlQuery);	
		$cONTACT->id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param CONTACTMySql cONTACT
 	 */
	public function update($cONTACT){
		$sql = 'UPDATE CONTACT SET HEADING_EN = ?, NAME_EN = ?, EMAIL_EN = ?, TEXT_EN = ?, BUTTON_EN = ?, HEADING_SV = ?, NAME_SV = ?, EMAIL_SV = ?, TEXT_SV = ?, BUTTON_SV = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($cONTACT->hEADINGEN);
		$sqlQuery->set($cONTACT->nAMEEN);
		$sqlQuery->set($cONTACT->eMAILEN);
		$sqlQuery->set($cONTACT->tEXTEN);
		$sqlQuery->set($cONTACT->bUTTONEN);
		$sqlQuery->set($cONTACT->hEADINGSV);
		$sqlQuery->set($cONTACT->nAMESV);
		$sqlQuery->set($cONTACT->eMAILSV);
		$sqlQuery->set($cONTACT->tEXTSV);
		$sqlQuery->set($cONTACT->bUTTONSV);

		$sqlQuery->set($cONTACT->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM CONTACT';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByHEADINGEN($value){
		$sql = 'SELECT * FROM CONTACT WHERE HEADING_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNAMEEN($value){
		$sql = 'SELECT * FROM CONTACT WHERE NAME_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEMAILEN($value){
		$sql = 'SELECT * FROM CONTACT WHERE EMAIL_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTEXTEN($value){
		$sql = 'SELECT * FROM CONTACT WHERE TEXT_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBUTTONEN($value){
		$sql = 'SELECT * FROM CONTACT WHERE BUTTON_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByHEADINGSV($value){
		$sql = 'SELECT * FROM CONTACT WHERE HEADING_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNAMESV($value){
		$sql = 'SELECT * FROM CONTACT WHERE NAME_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEMAILSV($value){
		$sql = 'SELECT * FROM CONTACT WHERE EMAIL_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByTEXTSV($value){
		$sql = 'SELECT * FROM CONTACT WHERE TEXT_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBUTTONSV($value){
		$sql = 'SELECT * FROM CONTACT WHERE BUTTON_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByHEADINGEN($value){
		$sql = 'DELETE FROM CONTACT WHERE HEADING_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNAMEEN($value){
		$sql = 'DELETE FROM CONTACT WHERE NAME_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEMAILEN($value){
		$sql = 'DELETE FROM CONTACT WHERE EMAIL_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTEXTEN($value){
		$sql = 'DELETE FROM CONTACT WHERE TEXT_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBUTTONEN($value){
		$sql = 'DELETE FROM CONTACT WHERE BUTTON_EN = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByHEADINGSV($value){
		$sql = 'DELETE FROM CONTACT WHERE HEADING_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNAMESV($value){
		$sql = 'DELETE FROM CONTACT WHERE NAME_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEMAILSV($value){
		$sql = 'DELETE FROM CONTACT WHERE EMAIL_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByTEXTSV($value){
		$sql = 'DELETE FROM CONTACT WHERE TEXT_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBUTTONSV($value){
		$sql = 'DELETE FROM CONTACT WHERE BUTTON_SV = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return CONTACTMySql 
	 */
	protected function readRow($row){
		$cONTACT = new CONTACT();
		
		$cONTACT->id = $row['id'];
		$cONTACT->hEADINGEN = $row['HEADING_EN'];
		$cONTACT->nAMEEN = $row['NAME_EN'];
		$cONTACT->eMAILEN = $row['EMAIL_EN'];
		$cONTACT->tEXTEN = $row['TEXT_EN'];
		$cONTACT->bUTTONEN = $row['BUTTON_EN'];
		$cONTACT->hEADINGSV = $row['HEADING_SV'];
		$cONTACT->nAMESV = $row['NAME_SV'];
		$cONTACT->eMAILSV = $row['EMAIL_SV'];
		$cONTACT->tEXTSV = $row['TEXT_SV'];
		$cONTACT->bUTTONSV = $row['BUTTON_SV'];

		return $cONTACT;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return CONTACTMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>